package qeug.shsh.helper;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.util.StringUtils;
import qeug.shsh.sso.SSOLoginPugin;

/**
 * @projectName: qeug-cosmic-root-project
 * @package: qeug.shsh.helper
 * @className: SysConfigHelper
 * @author: wenyx
 * @description: TODO
 * @date: 2023/10/16 14:57
 * @version: 1.0
 */
public class SsoHelper {
    //IAM域名
    private String SSO_DOMAIN;

    private static final Log log = LogFactory.getLog(SSOLoginPugin.class);
    //IAM
    private String client_id;
    private String client_secret ;//= "EFMS";

    private String back_url ;//= "EFMS";

    public String getSSO_DOMAIN() {
        return SSO_DOMAIN;
    }

    public String getClient_id() {
        return client_id;
    }

    public String getClient_secret() {
        return client_secret;
    }

    public String getBack_url() {
        return back_url;
    }

    public static String getToken(String url){
        log.info("sso开始获取token报文信息");
        String body = HttpRequest.post(url).execute().body();
        log.info("sso获取token报文信息"+body);
        if(StringUtils.isEmpty(body)){
            throw new RuntimeException("获取Token的返回信息为空");
        }
        JSONObject jsonObject = JSONObject.parseObject(body);
        String access_token = jsonObject.getString("access_token");
        if(StringUtils.isEmpty(access_token)){
            throw new RuntimeException("获取Token的返回信息为空");
        }
        return access_token;
    }

    public static String getUserId(String url){
        String body = HttpRequest.get(url).execute().body();
        log.info("sso获取用户报文信息"+body);
        if(StringUtils.isEmpty(body)){
            throw new RuntimeException("获取员工的返回信息为空");
        }
        JSONObject jsonObject = JSONObject.parseObject(body);
        String user_id = jsonObject.getString("id");
        if(StringUtils.isEmpty(user_id)){
            throw new RuntimeException("获取员工的返回信息为空");
        }
        return user_id;
    }

    public SsoHelper() {
        this.SSO_DOMAIN = System.getProperty("sso.desaddress");
        this.client_secret = System.getProperty("sso.client_secret");
        this.client_id = System.getProperty("sso.client_id");
        this.back_url = System.getProperty("sso.back_url");
    }
}
